﻿using AutoMapper;
using RestApp.UI.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using view = RestApp.UI.Models;

namespace RestApp.UI.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
		private IBusinessFactory _businessLogic;
		public HomeController(IBusinessFactory businessLogic)
		{
			_businessLogic = businessLogic;
		}

        public ActionResult Index()
        {
            if(User.Identity != null && User.IsInRole("User"))
            {
                var user = _businessLogic.UsersLogic.GetByLogin(User.Identity.Name);
                return RedirectToAction("CurrentUserCalendar", "Calendar");
            }
            else
            {
                return RedirectToAction("UserInfo");
            }
            //return View();
        }

        public ActionResult UserInfo()
        {
            var user = _businessLogic.UsersLogic.GetByLogin(User.Identity.Name);
            var mapperUser = Mapper.Map<RestApp.UI.Models.DetailedUser>(user);
            return View(mapperUser);
        }

		[HttpPost]
		public ActionResult Index(view.User user)
		{
			var businessEntity = user.ToBusinessEntity();
			_businessLogic.UsersLogic.Add(businessEntity);
			return View();
		}
    }
}